#import "YYY_ModelMethod_yyy.h"
#import <WebKit/WebKit.h>
#import "Kratos_Tool.h"

#import <WebKit/WebKit.h>

@interface YYY_ModelMethod_yyy()<WKNavigationDelegate,WKUIDelegate,UIScrollViewDelegate>

//****  判断是Teng还是Yun  ****
@property(nonatomic,assign) BOOL isYun;
//****  WKWebView  ****
@property (nonatomic,strong) WKWebView *YYY_WKWebView;

@end

@implementation YYY_ModelMethod_yyy

- (void)YYY_handleCustomAction_yyy:(NSURL *)URL{
    
    NSString *host = [URL host];
    
    if ([host isEqualToString:@"closeCallPage"]) {
        
        [self.delegate NNN_ModelCloseCallPageView_nnn];
        
    } else if ([host isEqualToString:@"callApp"]) {
        
        self.isYun = NO;
        
        NSDictionary *tempDic = [self YYY_urlToDic_yyy:URL];
        
        NSString *str = [tempDic objectForKey:@"url"];
        
        NSCharacterSet *encodeUrlSet = [NSCharacterSet URLQueryAllowedCharacterSet];
        str = [str stringByAddingPercentEncodingWithAllowedCharacters:encodeUrlSet];
        
        [self createWKWebViewToLoadURL:str ];
        
    } else if ([host containsString:@"callYun"] || [host containsString:@"liPa"]) {
        
        if ([URL.absoluteString componentsSeparatedByString:@"un?url="].count > 0 ||[URL.absoluteString componentsSeparatedByString:@"ay?url="].count > 0 )
        {
            NSArray *urlArr = [URL.absoluteString componentsSeparatedByString:@"?url="];
            
            NSString *str = urlArr.lastObject;
            
            self.isYun = YES;
            
            
            [self createWKWebViewToLoadURL:str];
        }
        
      
    }
    
}


- (void)YYY_closeMayunView_yyy{
    
    [self.YYY_WKWebView removeFromSuperview];
    self.YYY_WKWebView = nil;
}


- (void)createWKWebViewToLoadURL:(NSString *)url{
    
    self.YYY_WKWebView = [[WKWebView alloc] initWithFrame:CGRectZero];
    self.YYY_WKWebView.navigationDelegate = self;
    self.YYY_WKWebView.UIDelegate = self;
    [self.rootView.superview.superview addSubview:self.YYY_WKWebView];
    
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
    [self.YYY_WKWebView loadRequest:request];
    
}


#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    
    NSString *url = navigationAction.request.URL.absoluteString;
    
    
    if (self.isYun) {
        
        if ([url containsString:@"ay://al"]) {
            
            [Kratos_Tool Kratos_openScheme_ggg:url];
            [self YYY_closeMayunView_yyy];
            [self.delegate NNN_ModelCloseCallPageView_nnn];
            
            decisionHandler(WKNavigationActionPolicyCancel);
            return;
        }
        
        decisionHandler(WKNavigationActionPolicyAllow);
    }else{
        
        if ([url containsString:@"ixin:/"]) {
            [Kratos_Tool Kratos_openScheme_ggg:url];
            [self YYY_closeMayunView_yyy];
            [self.delegate NNN_ModelCloseCallPageView_nnn];
            decisionHandler(WKNavigationActionPolicyCancel);
            return;
        }
        
        NSDictionary *headers = [navigationAction.request allHTTPHeaderFields];
        BOOL hasReferer = [headers objectForKey:@"Referer"] != nil;
        if (hasReferer) {
            decisionHandler(WKNavigationActionPolicyAllow);
        } else {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    NSURL *url = [navigationAction.request URL];
                    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
                    [request setHTTPMethod:@"GET"];
                    [request setValue:@"game.lzgame.top/QYGame://" forHTTPHeaderField: @"Referer"];
                    [self.YYY_WKWebView loadRequest:request];
                });
            });
            decisionHandler(WKNavigationActionPolicyCancel);
            return;
        }
    }
}


#pragma mark -  url 字符串截取
- (NSDictionary *)YYY_urlToDic_yyy:(NSURL *)URL{
    
    NSArray *params =[URL.query componentsSeparatedByString:@"&"];
    
    NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
    for (NSString *paramStr in params) {
        NSArray *dicArray = [paramStr componentsSeparatedByString:@"="];
        if (dicArray.count > 1) {
            NSString *decodeValue = [dicArray[1] stringByRemovingPercentEncoding];
            [tempDic setObject:decodeValue forKey:dicArray[0]];
        }
    }
    return tempDic;
}

@end
